from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet

from question.models import Questionnaire
from question.serializers import QuestionnaireSerializer

from jwter.views import JWTER


class AccountViewSet(ModelViewSet):
    queryset = Questionnaire.objects.all()
    serializer_class = QuestionnaireSerializer


class ShareLink(APIView):
    def post(self):
        pass
        # 创建分享连接

    def get(self):
        pass
        # 访问question

    @staticmethod
    def linkMsgStruct(pid, sharer):
        return {"pid": pid, "sharer": sharer}

    def generate(self):
        data = self.linkMsgStruct()
        err, jid = self.jwter.create(data, "question")
        if err != -1:
            return jid
        return err


class ShareLinkBash:

    jwter = JWTER()

    def __init__(self, pid: int, sharer: int):
        self.pid = pid
        self.sharer = sharer

    def linkMsgStruct(self):
        return {"pid": self.pid, "sharer": self.sharer}

    def generate(self):
        data = self.linkMsgStruct()
        err, jid = self.jwter.create(data, "question")
        if err != -1:
            return jid
        return err
